package com.cccyy.multi_thread;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DownLatchTest {

    private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            64, 64, 0, TimeUnit.MINUTES, new ArrayBlockingQueue<>(32));

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 400; i++) {
            list.add(i);
        }
        for (int i = 0; i < 100; i++) {
            List<List<Integer>> sublist = Lists.partition(list, 400 / 32);
            int n = sublist.size();
            CountDownLatch countDownLatch = new CountDownLatch(n);
            System.out.println("执行外围循环：" + i);
            for (int j = 0; j < n; j++) {
                System.out.println("执行内部循环：" + j);
                threadPoolExecutor.execute(() -> {
                    try {
                        Thread.sleep(1000);
//                        System.out.println("任务执行。。。");
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                });
            }
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long count = countDownLatch.getCount();
            String s = String.valueOf(count);
            System.out.println("记录进程数还是count执行次数:" + s);
            System.out.println("===============>  详情任务 - 任务处理完成");
        }
        System.out.println("都执行完成了");

    }
}

/**
 * <dependency>
 * <groupId>com.google.guava</groupId>
 * <artifactId>guava</artifactId>
 * <version>31.1-jre</version>
 * </dependency>
 */
